﻿using BW.Mes.WinForms.APi;
using BW.Mes.WinForms.Dto;
using BW.Mes.WinForms.Properties;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static BW.Mes.WinForms.Dto.ShowExternalLabel;

namespace BW.Mes.WinForms.WareHouse.ExternalLabel
{
    public partial class ExternalLabelForm : DevExpress.XtraEditors.XtraForm
    {
        string baseUrl = Settings.Default.WareHouseUrl;

        public ExternalLabelForm()
        {
            InitializeComponent();
            GetExternalLabel();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 获取外部标签
        /// </summary>
        public async void GetExternalLabel()
         { 
            var  result = await HttpClientHepler.RequsetUrl(RequestType.get, MethodGolab.SHOW_EXTERNALLABEL);
            //反序列化
            var showexternal= JsonConvert.DeserializeObject<ApiPaing<ShowExternalLabel>>(result);
            if (showexternal.code == 200)
            {
                this.gridControl1.DataSource = showexternal.pageDatas;
            }
        }

        /// <summary>
        /// 删除外部标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton2_Click(object sender, EventArgs e)
        {
            var checkedIds = new List<long>();
            for (int rowHandle = 0; rowHandle < gridView1.RowCount; rowHandle++)
            {
                bool isChecked = false;
                var val = gridView1.GetRowCellValue(rowHandle, "IsSelect");
                if (val != null && val is bool)
                    isChecked = (bool)val;
                if (!isChecked) continue;
                var idObj = gridView1.GetRowCellValue(rowHandle, "id");
                if (idObj == null) continue;
                long id = Convert.ToInt64(idObj);
                checkedIds.Add(id);
            }
            if (checkedIds.Count == 0)
            {
                XtraMessageBox.Show("请先勾选要删除的行！");
                return;
            }
            if (XtraMessageBox.Show($"确认删除选中的 {checkedIds.Count} 条记录？", "批量删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                return;
            using (var client = new HttpClient { BaseAddress = new Uri(Settings.Default.WareHouseUrl) })
              {
                foreach (var id in checkedIds)
                {
                    var result = await HttpClientHepler.RequsetUrl(RequestType.post, MethodGolab.DELETE_EXTERNALLABEL+$"?Id={id}");
                   

                }
            }

            XtraMessageBox.Show("删除完成！");
            GetExternalLabel();
        }

            /// <summary>
            /// 新增外部标签按钮
            /// </summary>

            


            // 事件处理方法
            private void CreateForm_ExternalLabelAdded(object sender, EventArgs e)
            {
                // 刷新父窗体数据
                GetExternalLabel();
             }



        // 在父窗体中创建子窗体并订阅事件
        private void CreateExternal_Click(object sender, EventArgs e)
        {
            var createForm = new CreateExternalLabelForm();
            createForm.ExternalLabelAdded += CreateForm_ExternalLabelAdded;
            createForm.ShowDialog();

        }









    }
}